package com.sgb.jicai.view.activity;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import com.sgb.jicai.R;
import com.sgb.jicai.base.JiCaiBaseApi;
import com.sgb.jicai.databinding.ActivityMyJicaiSupervisionBinding;
import com.sgb.jicai.entity.JcMySupervisionEntity;
import com.sgb.jicai.view.adapter.JCMySupervisionAdapter;
import com.sgb.jicai.viewmodel.MyJicaiSupervisionModel;
import com.sgb.link.view.widget.PickCityPopWindow;
import com.swgk.core.base.AppActivity;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.xrecyclerview.XRecyclerView;
import java.util.ArrayList;
import java.util.List;

/**
 * 个人中心——集采管理——我的监督
 */
public class MyJicaiSupervisionActivity extends AppActivity {

    MyJicaiSupervisionModel model;
    private ActivityMyJicaiSupervisionBinding binding;
    private PickCityPopWindow typePopWindow;
    private String typeNo = "";
    private List<PickSelectEntity> typeDatas = new ArrayList<>();
    private PickCityPopWindow statusPopWindow;
    private String statusNo = "";
    private List<PickSelectEntity> statusDatas = new ArrayList<>();
    private JCMySupervisionAdapter adapter;
    private boolean isLoadMore = false;
    private int page = 1;

    public static void start(Context context) {
        Intent intent = new Intent(context, MyJicaiSupervisionActivity.class);
        context.startActivity(intent);
    }
    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_my_jicai_supervision);
        model = new ViewModelProvider(this).get(MyJicaiSupervisionModel.class);
        model.setActivity(MyJicaiSupervisionActivity.this);
        binding.ivBack.setOnClickListener(this::onMultiClick);
        binding.tvType.setOnClickListener(this::onMultiClick);
        binding.tvStatus.setOnClickListener(this::onMultiClick);

        model.getTypeList();
        model.getStatusList();
        initPopWindow();

        if (adapter == null) {
            adapter = new JCMySupervisionAdapter(getActivity(), null);
        }
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getActivity().getResources().getDimension(R.dimen.dp1), getActivity().getResources().getColor(R.color.color_F5F5F5)));
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                initFilterData();
            }

            @Override
            public void onLoadMore() {
                if (model.hasNext) {
                    isLoadMore = true;
                    page = page + 1;
                    initFilterData();
                } else {
                    binding.recyclerView.loadMoreComplete();
                }
            }
        });
        binding.recyclerView.setAdapter(adapter);

        adapter.setOnItemClick(new AdapterOnItemClick<JcMySupervisionEntity>() {
            @Override
            public void onItemClick(JcMySupervisionEntity entity, int position) {
                model.getH5Url(entity);
            }
        });
        adapter.setDeleteListener(new JCMySupervisionAdapter.DeleteListerner() {
            @Override
            public void delete(JcMySupervisionEntity entity) {
                //去h5处理
                model.getH5Url(entity);
            }
        });
    }

    public void showUrl(String url) {
        JiCaiBaseApi.startWeb(getActivity(), url);
    }

    @Override
    protected void init() {
        initFilterData();
    }

    private void initPopWindow() {
        //举报事项
        typePopWindow = new PickCityPopWindow(this);
        typePopWindow.setBgView(binding.grayLayout);
//        pickCityPopWindow.setOutsideTouchable(false);
//        pickCityPopWindow.setFocusable(false);
        typePopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                page = 1;
                typeNo = (String) key1;
                String name = (String) key2;
                initFilterData();
                if ("全部".equals(name)) {
                    binding.tvType.setText("举报事项");
                    binding.tvType.setTextColor(getResources().getColor(R.color.color_666666));
                    binding.ivType.setBackground(getResources().getDrawable(R.mipmap.ic_down_arrow_n));
                } else {
                    binding.tvType.setText(name);
                    binding.tvType.setTextColor(getResources().getColor(R.color.new_blue));
                    binding.ivType.setBackground(getResources().getDrawable(R.mipmap.ic_up_arrow_blue));
                }
            }
        });
        // 信息状态
        statusPopWindow = new PickCityPopWindow(this);
        statusPopWindow.setBgView(binding.grayLayout);
//        statusPopWindow.setOutsideTouchable(false);
//        statusPopWindow.setFocusable(false);
        statusPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                page = 1;
                statusNo = (String) key1;
                String name = (String) key2;
                initFilterData();
                if ("全部".equals(name)) {
                    binding.tvStatus.setText("信息状态");
                    binding.tvStatus.setTextColor(getResources().getColor(R.color.color_666666));
                    binding.ivStatus.setBackground(getResources().getDrawable(R.mipmap.ic_down_arrow_n));
                } else {
                    binding.tvStatus.setText(name);
                    binding.tvStatus.setTextColor(getResources().getColor(R.color.new_blue));
                    binding.ivStatus.setBackground(getResources().getDrawable(R.mipmap.ic_up_arrow_blue));
                }
            }
        });
    }

    //刷新列表数据
    private void initFilterData() {
        model.getList(typeNo, statusNo, page);
    }

    //刷新收据之后更新列表，设置是否可加载更多
    public void showList(List<JcMySupervisionEntity> list, boolean hasNext) {
        binding.recyclerView.refreshComplete();
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list.size() > 0) {
                adapter.addDatas(list);
            } else {
//                binding.recyclerView.setLoadingMoreEnabled(false);
            }
            binding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            binding.recyclerView.refreshComplete();
            if (list.size() > 0) {
                binding.recyclerView.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                adapter.setDatas(list);
//                binding.recyclerView.setLoadingMoreEnabled(true);
            } else {
                adapter.setDatas(null);
                binding.recyclerView.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
        //设置是否可加载更多：
        binding.recyclerView.setNoMore(!hasNext);
        if (adapter.getItemCount() >= 3) {
            binding.recyclerView.setNoMore(!hasNext);
        } else {
            binding.recyclerView.setLoadingMoreEnabled(hasNext);
        }
    }

    @Override
    public void onMultiClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.tv_type) {
            dismissPop();
            showTypePopWindow();
        } else if (id == R.id.tv_status) {
            dismissPop();
            showStatusPopWindow();
        }
    }

    private void dismissPop() {
        typePopWindow.dismiss();
        statusPopWindow.dismiss();
    }

    private void showStatusPopWindow() {
        statusPopWindow.showPopwindow(binding.tvStatus);
        statusPopWindow.setData(statusDatas);
    }

    private void showTypePopWindow() {
        typePopWindow.showPopwindow(binding.tvType);
        typePopWindow.setData(typeDatas);
    }

    //接口调用
    public void setStatusDatas(List<PickSelectEntity> datas) {
        statusDatas.clear();
        statusDatas.addAll(datas);
    }

    //接口调用
    public void setTypeDatas(List<PickSelectEntity> datas) {
        typeDatas.clear();
        typeDatas.addAll(datas);
    }


}
